import { app, BrowserWindow } from "electron";
import { fileURLToPath } from "node:url";
import path from "node:path";
import BetterSqlite3 from "better-sqlite3";
const __app_root = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
const getExeDir = () => {
  return app.isPackaged ? path.dirname(process.execPath) : __app_root;
};
const getResourcesDirPath = () => {
  return path.join(getExeDir(), "./resources");
};
function initialize(db) {
  console.log(" init table --> app_para_config");
  db.exec(`
        CREATE TABLE IF NOT EXISTS app_para_config (
          id INTEGER PRIMARY KEY AUTOINCREMENT,
          name TEXT NOT NULL,
          email TEXT UNIQUE NOT NULL,
          created_at DATETIME DEFAULT CURRENT_TIMESTAMP
        )
    `);
}
function query(db, sql, params = []) {
  try {
    const stmt = db.prepare(sql);
    if (sql.trim().toUpperCase().startsWith("SELECT")) {
      return params.length > 0 ? stmt.all(params) : stmt.all();
    } else {
      return params.length > 0 ? stmt.run(params) : stmt.run();
    }
  } catch (error) {
    console.error("Database error:", error);
    throw error;
  }
}
const APP_ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), "..");
const useSQLite3 = () => {
  const resource = getResourcesDirPath();
  console.log(path.join(resource, "app.db"), "98989898");
  const db = new BetterSqlite3(path.join(resource, "better-sqlite3.sqlite3"), {
    // https://github.com/WiseLibs/better-sqlite3/blob/v8.5.2/lib/database.js#L36
    // https://github.com/WiseLibs/better-sqlite3/blob/v8.5.2/lib/database.js#L50
    nativeBinding: path.join(APP_ROOT, "dist-native/better_sqlite3.node")
  });
  db.pragma("journal_mode = WAL");
  function find(sql, params = []) {
    return query(db, sql, params);
  }
  initialize(db);
  return {
    db,
    find,
    info: "success"
  };
};
const __dirname = path.dirname(fileURLToPath(import.meta.url));
process.env.APP_ROOT = path.join(__dirname, "..");
const VITE_DEV_SERVER_URL = process.env["VITE_DEV_SERVER_URL"];
const MAIN_DIST = path.join(process.env.APP_ROOT, "dist-electron");
const RENDERER_DIST = path.join(process.env.APP_ROOT, "dist");
process.env.VITE_PUBLIC = VITE_DEV_SERVER_URL ? path.join(process.env.APP_ROOT, "public") : RENDERER_DIST;
debugger;
let win;
function createWindow() {
  win = new BrowserWindow({
    icon: path.join(process.env.VITE_PUBLIC, "electron-vite.svg"),
    webPreferences: {
      preload: path.join(__dirname, "preload.mjs")
    }
  });
  if (VITE_DEV_SERVER_URL) {
    win.loadURL(VITE_DEV_SERVER_URL);
  } else {
    win.loadFile(path.join(RENDERER_DIST, "index.html"));
  }
}
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
    win = null;
  }
});
app.on("activate", () => {
  if (BrowserWindow.getAllWindows().length === 0) {
    createWindow();
  }
});
app.whenReady().then(() => {
  createWindow();
  setTimeout(() => {
    useSQLite3();
    win == null ? void 0 : win.webContents.on("did-finish-load", () => {
      win == null ? void 0 : win.webContents.send("main-process-message", (/* @__PURE__ */ new Date()).toLocaleString());
    });
  }, 500);
});
export {
  MAIN_DIST,
  RENDERER_DIST,
  VITE_DEV_SERVER_URL
};
